﻿using System;
using System.Collections.Generic;

using SBPweb.Communication.Shared.Library.Contacts;

namespace SBPweb.Communication.Shared.Library.Conversations
{

    public abstract class ConversationBase : IConversation
    {

        #region Private fields

        private IContact owner;
        private IContact receipient;
        private List<IContact> members;

        #endregion

        #region Protected fields

        protected bool isConference;

        #endregion

        #region Constructor logic

        protected ConversationBase(IContact owner, IContact receipient)
            : this(owner, receipient, new List<IContact>(new IContact[] { owner, receipient }))
        {
        }

        protected ConversationBase(IContact owner, IContact receipient, List<IContact> members)
        {
            this.owner = owner;
            this.receipient = receipient;
            this.members = members;
            if (!members.Contains(owner))
            {
                members.Add(owner);
            }
        }

        #endregion

        #region Property accessors

        public List<IContact> Members
        {
            get
            {
                return members;
            }
        }

        public IContact Owner
        {
            get
            {
                return owner;
            }
        }

        public IContact Receipient
        {
            get
            {
                return receipient;
            }
        }

        public virtual bool IsConference
        {
            get
            {
                return isConference;
            }
        }

        public string Title
        {
            get;
            set;
        }

        #endregion

        #region Events

        public event EventHandler<MessageBaseEventArgs> MessageSent;

        public event EventHandler<MessageBaseEventArgs> MessageReceived;

        #endregion

        #region Methods

        public abstract void SendMessage(string body);

        public abstract void SendMessage(MessageBase message);

        protected void OnMessageReceive(object sender, MessageBaseEventArgs e)
        {
            if (MessageReceived != null)
            {
                MessageReceived(sender, e);
            }
        }

        #endregion

    }

}